package com.ffcc.fitness.merchant.service.impl;

import com.ffcc.fitness.common.service.QRCodeService;
import com.ffcc.fitness.merchant.mapper.MerchantMapper;
import com.ffcc.fitness.merchant.pojo.dto.MerchantUpdateParam;
import com.ffcc.fitness.merchant.pojo.entity.Merchant;
import com.ffcc.fitness.merchant.pojo.vo.MerchantInfo;
import com.ffcc.fitness.merchant.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    MerchantMapper merchantMapper;
    
    @Autowired
    private QRCodeService qrCodeService;

    @Override
    public Merchant update(MerchantUpdateParam merchantUpdateParam) {
        //接收的前端传来的参数
        log.info("修改教练信息，参数：{}", merchantUpdateParam);
        Merchant merchant = merchantMapper.selectByUsername(merchantUpdateParam.getUsername());
        BeanUtils.copyProperties(merchantUpdateParam, merchant);
        merchantMapper.update(merchant);
        //修改完后，把新的用户信息返回
        return merchant;
    }    @Override
    public Merchant selectByMerchantId(Long merchantId) {
        log.info("根据商家ID查询商家信息: merchantId={}", merchantId);
        return merchantMapper.selectByMerchantId(merchantId);
    }

    @Override
    public Merchant selectByUserId(Long userId) {
        log.info("根据用户ID查询商家信息: userId={}", userId);
        
        // 使用selectByUserId查询，然后转换为Merchant实体
        MerchantInfo merchantInfo = merchantMapper.selectByUserId(userId);
        if (merchantInfo == null) {
            log.warn("用户ID {}没有对应的商家信息", userId);
            return null;
        }
        
        // 将MerchantInfo转换为Merchant实体
        Merchant merchant = new Merchant();
        merchant.setMerchantId(merchantInfo.getMerchantId());
        merchant.setUserId(merchantInfo.getUserId());
        merchant.setName(merchantInfo.getName());
        merchant.setLicense(merchantInfo.getLicense());
        merchant.setDescription(merchantInfo.getDescription());
        merchant.setQrCode(merchantInfo.getQrCode());
        
        return merchant;
    }

    @Override
    @Transactional
    public String generateQRCodeForMerchant(Long merchantId) {
        log.info("为商家生成QR码: merchantId={}", merchantId);
        
        // 1. 查询商家信息
        Merchant merchant = merchantMapper.selectByMerchantId(merchantId);
        if (merchant == null) {
            throw new RuntimeException("商家不存在: " + merchantId);
        }
        
        // 2. 如果已有QR码，直接返回
        if (merchant.getQrCode() != null && !merchant.getQrCode().trim().isEmpty()) {
            log.info("商家已有QR码，直接返回: merchantId={}, qrCode={}", merchantId, merchant.getQrCode());
            return merchant.getQrCode();
        }
        
        // 3. 生成新的QR码
        String qrCodeUrl = qrCodeService.generateMerchantQRCode(merchantId, merchant.getName());
        
        // 4. 更新商家QR码
        updateMerchantQRCode(merchantId, qrCodeUrl);
        
        log.info("商家QR码生成并保存成功: merchantId={}, qrCode={}", merchantId, qrCodeUrl);
        return qrCodeUrl;
    }
      @Override
    @Transactional
    public void updateMerchantQRCode(Long merchantId, String qrCodeUrl) {
        log.info("更新商家QR码: merchantId={}, qrCodeUrl={}", merchantId, qrCodeUrl);
        
        // 使用新的根据merchantId更新QR码的方法
        merchantMapper.updateQRCodeByMerchantId(merchantId, qrCodeUrl);
        
        log.info("商家QR码更新成功: merchantId={}", merchantId);
    }

    @Override
    @Transactional
    public String generateQRCodeForUser(Long userId) {
        log.info("为用户生成商家QR码: userId={}", userId);
        
        // 1. 查询商家信息
        Merchant merchant = selectByUserId(userId);
        if (merchant == null) {
            throw new RuntimeException("用户ID对应的商家不存在: " + userId);
        }
        
        // 2. 如果已有QR码，直接返回
        if (merchant.getQrCode() != null && !merchant.getQrCode().trim().isEmpty()) {
            log.info("商家已有QR码，直接返回: userId={}, merchantId={}, qrCode={}", 
                    userId, merchant.getMerchantId(), merchant.getQrCode());
            return merchant.getQrCode();
        }
        
        // 3. 生成新的QR码
        String qrCodeUrl = qrCodeService.generateMerchantQRCode(merchant.getMerchantId(), merchant.getName());
        
        // 4. 更新商家QR码
        updateMerchantQRCode(merchant.getMerchantId(), qrCodeUrl);
          log.info("用户商家QR码生成并保存成功: userId={}, merchantId={}, qrCode={}", 
                userId, merchant.getMerchantId(), qrCodeUrl);
        return qrCodeUrl;
    }

    @Override
    @Transactional
    public String generateOrderQRCodeForUser(Long userId, String equipmentName, Integer quantity, String totalAmount) {
        log.info("为用户生成订单QR码: userId={}, equipmentName={}, quantity={}, totalAmount={}", 
                userId, equipmentName, quantity, totalAmount);
        
        // 1. 查询商家信息
        Merchant merchant = selectByUserId(userId);
        if (merchant == null) {
            throw new RuntimeException("用户ID对应的商家不存在: " + userId);
        }
        
        // 2. 生成包含订单信息的动态QR码
        String qrCodeUrl = qrCodeService.generateOrderQRCode(merchant.getMerchantId(), 
                merchant.getName(), equipmentName, quantity, totalAmount);
        
        log.info("订单QR码生成成功: userId={}, merchantId={}, qrCode={}", 
                userId, merchant.getMerchantId(), qrCodeUrl);
        return qrCodeUrl;
    }
}
